package com.tanhua.app.service;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.date.DateUtil;
import com.tanhua.api.db.UserInfoApi;
import com.tanhua.api.mongdb.CommentApi;
import com.tanhua.api.mongdb.FriendApi;
import com.tanhua.domain.UserHolder;
import com.tanhua.domain.db.UserInfo;
import com.tanhua.domain.mogo.Comment;
import com.tanhua.domain.mogo.Friend;
import com.tanhua.domain.vo.ContactVo;
import com.tanhua.domain.vo.PageBeanVo;
import com.tanhua.domain.vo.UserCommentVo;
import com.tanhua.template.HuanXinTemplate;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class MessageService {

    @Reference
    private FriendApi friendApi;

    @Autowired
    private HuanXinTemplate huanXinTemplate;
    @Reference
    private UserInfoApi userInfoApi;
    @Reference
    private CommentApi commentApi;

    public ResponseEntity contacts(long userId, Long id) {
                            // id：自己的 userId：对方的
        friendApi.contacts(id,userId); //探花业务加好友
        huanXinTemplate.addContacts(id,userId); // 环信业务加好友
        return ResponseEntity.ok(null);
    }

    public ResponseEntity findFriendPage(Integer page, Integer pagesize, Long id) {
        //分页查询friend数据
        PageBeanVo pageBean=friendApi.findFriendPage(page,pagesize,id);
        //根据friend的friendId查询userInfo
        List<Friend> items = (List<Friend>) pageBean.getItems();

        List<ContactVo> contactVoList=new ArrayList<>();
        if(CollectionUtil.isNotEmpty(items)){
            for (Friend friend : items) {
                UserInfo userInfo = userInfoApi.findByUser(friend.getFriendId());
                //组装ContactVo
                ContactVo vo = new ContactVo();
                BeanUtils.copyProperties(userInfo,vo);
                vo.setUserId(userInfo.getId().toString());
                vo.setCity(userInfo.getCity().split("-")[0]);
                contactVoList.add(vo);
            }
        }
        pageBean.setItems(contactVoList);

        //返回
        return ResponseEntity.ok(pageBean);
    }

    public ResponseEntity likes(Integer page, Integer pagesize, int commentType) {

        // 查询点赞用户的所有人信息
        PageBeanVo pageBean=commentApi.findCommentPage(page,pagesize,commentType, UserHolder.findUser().getId());
        // 查询userInfo
        List<Comment> items = (List<Comment>) pageBean.getItems();

        List<UserCommentVo> userCommentVoList=new ArrayList<UserCommentVo>();
        if(CollectionUtil.isNotEmpty(items)) {
            for (Comment comment : items) {
                UserInfo userInfo = userInfoApi.findByUser(comment.getUserId());
                //组装app需要的UserCommentVo
                UserCommentVo vo = new UserCommentVo();
                BeanUtils.copyProperties(userInfo, vo);
                vo.setCreateDate(DateUtil.format(new Date(comment.getCreated()), "yyyy-MM-dd HH:mm:ss"));
                userCommentVoList.add(vo);
            }
        }
        // 返回pageBean
        pageBean.setItems(userCommentVoList);
        return ResponseEntity.ok(pageBean);
    }
}
